package com.eci.common.asset.core;

import java.io.Serializable;

import com.eci.common.asset.constant.StringConstant.Symbol;
import com.eci.common.asset.enums.ScodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class R<T extends Page> implements Serializable {

    private static final long serialVersionUID = 1L;


    /**
     * 状态
     */
    private Boolean status;
    /**
     * 状态码
     */
    private String scode;
    /**
     * 消息内容
     */
    private String message;
    /**
     * 数据
     */
    @JsonInclude(Include.NON_NULL)
    private T data = null;


    private R() {

    }

    private R(String scode, String message) {
        this.status = ScodeEnum.SUCCESS.getCode().equals(scode);
        this.scode = scode;
        this.message = message;
    }

    private R(String scode, String message, T data) {
        this(scode, message);
        this.data = data;
    }

    private static class Singletons {
        private static final R INSTANCE = new R(ScodeEnum.SUCCESS.getCode(), ScodeEnum.SUCCESS.getMessage());
    }

    /**
     * 返回成功提示(without data)
     * 提示内容是"操作成功"。
     */
    public static R ok() {
        return Singletons.INSTANCE;
    }


    /**
     * 返回成功提示(without data)
     *
     * @param message 提示消息内容
     */
    public static R ok(String message) {
        return new R(ScodeEnum.SUCCESS.getCode(), message);
    }


    /**
     * 返回成功提示(with data)
     *
     * @param data 详细数据
     */
    public static <M extends Page> R<M> ok(Page data) {
        return new R(ScodeEnum.SUCCESS.getCode(), ScodeEnum.SUCCESS.getMessage(), data);
    }


    /**
     * 返回成功提示(with data)
     *
     * @param message 提示消息内容
     * @param data    详细数据
     */
    public static <M extends Page> R<M> ok(String message, Page data) {
        return new R(ScodeEnum.SUCCESS.getCode(), message, data);
    }


    /**
     * 返回失败提示(without data)
     *
     * @param failMsg 自定义失败的消息内容
     */
    public static R fail(String failMsg) {
        return new R(ScodeEnum.CUSTOM_EXCEPTION.getCode(), failMsg);
    }


    /**
     * 返回失败提示(without data)
     *
     * @param scodeEnum 失败类型枚举
     * @param failMsg   自定义失败的消息内容
     */
    public static R fail(ScodeEnum scodeEnum, String failMsg) {
        return new R(scodeEnum.getCode(), scodeEnum.getMessage() + Symbol.COLON + failMsg);
    }


    /**
     * 返回失败提示(with data)
     *
     * @param scodeEnum 失败类型枚举
     * @param failMsg   自定义失败的消息内容
     */
    public static <M extends Page> R<M> fail(ScodeEnum scodeEnum, String failMsg, Page data) {
        return new R(scodeEnum.getCode(), scodeEnum.getMessage() + Symbol.COLON + failMsg, data);
    }


    public static R fail(String scode, String message) {
        return new R(scode, message);
    }


    public Boolean getStatus() {
        return status;
    }

    public String getScode() {
        return scode;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

}
